from rest_framework import serializers
from .models import LearningProgress, LearningHistory

class LearningProgressSerializer(serializers.ModelSerializer):
    class Meta:
        model = LearningProgress
        fields = ['id', 'user', 'content_type', 'content_id', 'progress_percentage', 'is_completed', 
                  'learning_time_seconds', 'last_learning_time', 'created_at', 'updated_at']
        read_only_fields = ['id', 'user', 'created_at', 'updated_at']

class LearningHistorySerializer(serializers.ModelSerializer):
    class Meta:
        model = LearningHistory
        fields = ['id', 'user', 'content_type', 'content_id', 'start_time', 'end_time', 'duration_seconds']
        read_only_fields = ['id', 'user']

# 用于更新学习进度的序列化器
class UpdateProgressSerializer(serializers.ModelSerializer):
    class Meta:
        model = LearningProgress
        fields = ['progress_percentage', 'is_completed', 'learning_time_seconds']
        read_only_fields = ['id', 'user', 'content_type', 'content_id', 'created_at', 'updated_at']

# 用于获取学习统计信息的序列化器
class LearningStatisticsSerializer(serializers.Serializer):
    total_learning_time_seconds = serializers.IntegerField()
    completed_items = serializers.IntegerField()
    total_items = serializers.IntegerField()
    progress_rate = serializers.FloatField()